package cn.ecut.dispatcher;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author lujiapeng
 * @className DispatcherForwardServlet
 * @description TODO
 * @date 2020/9/14 8:26
 **/
@WebServlet("/forward")
public class DispatcherForwardServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");

        PrintWriter writer = response.getWriter();
        writer.write("我是外部的 Servlet 01 ");

        // 获取的第一种方式 : request.getRequestDispatcher
        // 此时是将资源封装到了 RequestDispatcher 对象中
        RequestDispatcher requestDispatcher = request.getRequestDispatcher("/innerServlet");

//        requestDispatcher = this.getServletContext().getRequestDispatcher("/innerServlet") ;
//
//        requestDispatcher = this.getServletContext().getNamedDispatcher("/innerServlet") ;


        // 进行转发
        requestDispatcher.forward(request, response);
        System.out.println("request#hashcode" + request.hashCode());
        System.out.println("response#hashcode" + response.hashCode());
        writer.write("我是外部的 Servlet 02 ");
    }
}
